//
//  RootViewController.m
//  万词王
//
//  Created by rimi on 2016/11/29.
//  Copyright © 2016年 dongfumei. All rights reserved.
//

#import "RootViewController.h"
#import "AllViewController.h"
#import "WordViewController.h"
#import "WordDicViewController.h"
#import "IdiomViewController.h"

@interface RootViewController (){
    NSInteger _pressedIndex;
}

@property (nonatomic, strong) UIImageView *bgImage;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIButton *searchButton;
@property (nonatomic, strong) UIView *searchView;
@property (nonatomic, strong) UIButton *searchBtn;
@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self initWith];
    
}

-(void)initWith{
    [self.view addSubview:self.bgImage];
    [self.view addSubview:self.nameLabel];
    [self.view addSubview:self.searchButton];
    
    for (int i = 0; i<3; i++) {
        UIButton *button = [[UIButton alloc]initWithFrame:AAdaptionRect(CGRectGetMinX(self.searchButton.frame)+5+(300*i/3 + 15*i), CGRectGetMaxY(self.searchButton.frame)/AAdaptationWidth()+20, 60/AAdaptationWidth(), 60)];
        [button setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        button.tag = 1001+i;
        button.backgroundColor = [UIColor purpleColor];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }
    
    [self.view addSubview:self.searchView];
    [self.searchView addSubview:self.searchBtn];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(gestureClick)];
    
    [self.searchView addGestureRecognizer:tapGesture];
}

-(void)gestureClick{
    
    [UIView animateWithDuration:0.8 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
        _searchView.frame = AAdaptionRect(-kBaseWidth-20, 0, kBaseWidth, kBaseHeight);
    } completion:^(BOOL finished) {
        
    }];
}

-(void) searchBtnClick :(UIButton *)sender {
    
    if (_pressedIndex == 1) {
        WordViewController *word = [[WordViewController alloc]init];
        [self presentViewController:word animated:NO completion:nil];
    }else if (_pressedIndex == 2){
        WordDicViewController *words = [[WordDicViewController alloc]init];
        [self presentViewController:words animated:NO completion:nil];
    }else if (_pressedIndex == 3) {
        IdiomViewController *idiom = [[IdiomViewController alloc]init];
        [self presentViewController:idiom animated:NO completion:nil];
    }
}

-(void)buttonClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    
    switch (sender.tag) {
        case 1001:{
            _pressedIndex = 1;
        }
            break;
        case 1002:{
            _pressedIndex = 2;
        }
            break;
        case 1003:{
            _pressedIndex = 3;
        }
            break;
            
        default:
            break;
    }
    [UIView animateWithDuration:0.8 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        _searchView.frame = AAdaptionRect(0, 0, kBaseWidth, kBaseHeight);
    } completion:^(BOOL finished) {
        
    }];
}

-(void)searchButtonClick{
    
    AllViewController *all = [[AllViewController alloc]init];
    [self presentViewController:all animated:NO completion:nil];
}

-(UIImageView *)bgImage{
    if (!_bgImage) {
        _bgImage = [[UIImageView alloc]initWithFrame:AAdaptionRect(0, 0, kBaseWidth/AAdaptationWidth(), kBaseHeight)];
        _bgImage.image = [UIImage imageNamed:@"1.jpg"];
    }
    return _bgImage;
}

-(UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]initWithFrame:AAdaptionRect((kBaseWidth - 100)/2, 150, 100, 50)];
        _nameLabel.text = @"ghost";
        _nameLabel.textColor = [UIColor whiteColor];
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        _nameLabel.font = [UIFont systemFontOfSize:28];
        
    }
    return _nameLabel;
}

-(UIButton *)searchButton{
    if (!_searchButton) {
        _searchButton = [[UIButton alloc]initWithFrame:AAdaptionRect((kBaseWidth - 300)/2, CGRectGetMaxY(self.nameLabel.frame)+20, 300, 44)];
        [_searchButton setTitle:@"🔍字、词、成语都可查询" forState:UIControlStateNormal];
        _searchButton.backgroundColor = [UIColor whiteColor];
        [_searchButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        _searchButton.layer.cornerRadius = 10;
        _searchButton.layer.masksToBounds = YES;
        [_searchButton addTarget:self action:@selector(searchButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchButton;
}

-(UIView *)searchView{
    if (!_searchView) {
        _searchView = [[UIView alloc]initWithFrame:AAdaptionRect(-kBaseWidth, 0, kBaseWidth, kBaseHeight)];
        _searchView.backgroundColor = [UIColor darkGrayColor];
        _searchView.userInteractionEnabled = YES;
    }
    return _searchView;
}

-(UIButton *)searchBtn{
    if (!_searchBtn) {
        _searchBtn = [[UIButton alloc]initWithFrame:AAdaptionRect(50, 150, kBaseWidth - 100/AAdaptationWidth(), 50)];
        _searchBtn.backgroundColor = [UIColor whiteColor];
        _searchBtn.layer.cornerRadius = 10;
        _searchBtn.layer.masksToBounds = YES;
        [_searchBtn addTarget:self action:@selector(searchBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchBtn;
}


@end
